package com.misweb.ibase;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.util.List;

public class HttpBasicInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
        // do something  发送之前添加cookie
        if(request.getHeaders().containsKey("Cookie")){
            //do nothing

        }else{
            if(GlobalData.getCookieString()!=null){
                request.getHeaders().add("Cookie", GlobalData.getCookieString());

            }
        }
/*            // do something  发送之前添加cookie
            if(request.getHeaders().containsKey("Cookie")){
                //do nothing
                Log.d("HttpBasicInterceptor:beforeCookie",request.getHeaders().get("Cookie").get(0));
            }*/


        ClientHttpResponse temp= execution.execute(request, data);

        //返回之后如果发现set-cookie  则重设cookie
        List<String> allCookies = temp.getHeaders().get("Set-Cookie");
        if (allCookies!= null) {
            GlobalData.changeCookiesFromNet(new Cookies(allCookies));
        }
        return temp;
    }



}

